<template>
  <div>
    <!-- 顶部导航 -->
    <van-nav-bar class='page-nav-bar' title="登录">
      <van-icon slot='left' name="cross" class='left-icon' @click='$router.go(-1)' />
    </van-nav-bar>
    <!-- 表单 -->
    <van-form @submit="onSubmit" ref='loginForm'>
      <van-field v-model="mobile" name="mobile" placeholder="请输入手机号" :rules='userFormRules.mobiile' type="number"
        maxlength="11">
        <i slot='left-icon' class='toutiao toutiao-shouji'></i>
      </van-field>
      <van-field v-model="code" name="code" placeholder="请输入验证码" :rules='userFormRules.code' type="number"
        maxlength="6">
        <i slot='left-icon' class='toutiao toutiao-yanzhengma'> </i>
        <template #button>
          <van-count-down v-if='isShowCountDown' @finish='isShowCountDown = false' :time="6*1000" format="ss s" />
          <van-button v-else @click='loginCode' native-type="button" class="send-sms-btn" size="small" type="primary">
            发送验证码
          </van-button>
        </template>
      </van-field>
      <div class='login-btns'>
        <van-button round block type="info" native-type="submit">提交</van-button>
      </div>
    </van-form>
  </div>
</template>

<script>
import { setItem } from '@/utils/storage'
// 登录请求 验证码请求
import { login, sendCode } from '@/api/user.js'
export default {
  data () {
    return {
      // 倒计时显示
      isShowCountDown: false,
      // 手机号
      mobile: '',
      // 验证码
      code: '',
      // 验证规则
      userFormRules: {
        mobiile: [{
          required: true, message: '请输入手机号'
        }, {
          pattern: /^1[3|5|7|8]\d{9}$/, message: '手机格式不正确'
        }],
        code: [
          {
            required: true, message: '请输入验证码'
          }, {
            pattern: /^\d{6}$/, message: '验证码不正确'
          }
        ]
      }
    }
  },
  methods: {
    // 登录时执行
    async onSubmit () {
      // 加载提示
      this.$toast.loading({
        duration: 0, // 持续展示 toast
        message: '加载中...',
        forbidClick: true
      })
      // 登录成功时执行
      try {
        const { data: res } = await login({
          mobile: this.mobile,
          code: this.code
        })
        // 保存秘钥
        this.$store.commit('lintUser', res.data)
        this.$toast.success('登录成功')
        this.$router.go(-1)
        // 登录失败时执行
      } catch (err) {
        if (err.response && err.response.statues === 400) {
          return this.$toast.fail('账号或者密码错误')
        }
        this.$toast.fail('服务器异常,登录失败')
      }
    },
    // 点击发送验证码
    async loginCode () {
      try {
        await this.$refs.loginForm.validate('mobile')
      } catch (err) {
        return
      }
      // 加载提示
      this.$toast.loading({
        duration: 0, // 持续展示 toast
        message: '发送中...',
        forbidClick: true
      })
      try {
        await sendCode(this.mobile)
        this.isShowCountDown = true
        this.$toast.success('发送成功')
      } catch (err) {
        if (err.response && err.response.status === 429) {
          return this.$toast.fail('发送频繁')
        }
        this.$toast.fail('发送失败')
      }
    }
  },
  computed: {},
  watch: {},
  filters: {},
  components: {},
  created () { },
  mounted () { }
}
</script>

<style scoped lang='less'>
.toutiao {
  font-size: 34px;
}
.send-sms-btn {
  width: 152px;
  height: 46px;
  line-height: 46px;
  background-color: #ededed;
  font-size: 22px;
  color: #666;
  border: none;
}
.login-btns {
  margin: 53px 28px;
  .van-button {
    width: 694px;
    height: 88px;
    background-color: #6db4fb;
    border: none;
  }
}
</style>
